﻿$(document).ready(function () {
    $("#ul-nav li:not(.nav-header)").eq(7).addClass("active");
    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 10,
    url: "/handler/admin/lottery.ashx?action=",
    popU: null,

    init: function () {
        page.getList();
    },

    getList: function (pageIndex) {
        var title = $.trim($("#title").val());
        var spstatus = $("#spstatus").val();

        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        $.get(page.url + "getList", { pageIndex: page.pageIndex, pageSize: page.pageSize, spstatus: spstatus, title: title, start: $("#start").val(), end: $("#end").val(), r: Math.random()
        }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("auctionlist", data));
            page.pager(data.count);
            page.list = data.list;
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.getList
        });
    },

    tStatus: function (status, end) {
        if (status == 1)
            return "已结束";
        var now = new Date();
        if (end.ToDate() >= now)
            return "进行中";
        return "待开奖";
    },

    joinCount: function (participants) {
        if (participants == undefined)
            return 0;
        var array = participants.split(',');
        var count = 0;
        for (var i = 0; i < array.length; i++) {
            if (array[i] != "")
                count++;
        }
        return count;
    },

    edit: function (id) {
        page.ajaxUploadObj = null;
        var auction = null;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == id) {
                auction = page.list[i];
                break;
            }
        }

        if (auction == null) {
            alert("数据异常，请刷新页面后再试");
            return false;
        }

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "编辑抽奖基本信息",
            contents: TrimPath.processDOMTemplate("detail", auction),
            w: 750,
            drag: true,
            y: $(window).scrollTop() + 30
        });
        page.popU.showPop();

        $("#spnTitle").html('<textarea id="utitle" class="span7" placeholder="主题" type="text" required rows="5">' + auction.Title + '</textarea>');

        $("#btnEdit").unbind("click").click(function () {
            page.editAuction(auction.Id);
        });

        page.initUpload();
    },

    initUpload: function () {
        page.ajaxUploadObj = new AjaxUpload('upload', {
            action: "/handler/upload.ashx?action=uploadFile",
            autoSubmit: true,
            data: {},
            onSubmit: function (file, ext) {
                $("#btnAdd").unbind("click").click(function () { alert("Logo上传中，请等图片上传后再操作"); });
                this.disable();
            },
            onChange: function (file, ext) {
                $("#ulogo").val(file);
                this.enable();
            },
            responseType: 'json',
            onComplete: function (file, response) {
                if (response.error) {
                    alert(response.error);
                    $("#ulogo").val('');
                }
                else {
                    $("#ulogo").val(response.url);
                }
                $("#btnAdd").unbind("click").click(page.submit);
                this.enable();
            }
        });
    },

    editAuction: function (id) {
        var title = $.trim($("#utitle").val());
        if (title == "") {
            alert("请输入主题");
            $("#utitle").focus();
            return false;
        }

        var logo = $("#ulogo").val();
        if ($.trim(logo) == "") {
            alert("请输入logo url地址或者上传本地图片");
            $("#ulogo").focus();
            return false;
        }

        var end = $("#uend").val();
        if ($.trim(end) == "") {
            alert("请选择结束时间");
            $("#uend").focus();
            return false;
        }

        $.post(page.url + "edit", { id: id, title: title, logo: logo, end: end }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getList();
            page.popU.close();
        }, "json");
    },

    showList: function (data) {
        var html = [];
        $.each(data.split(','), function () {
            if ($.trim(this) != "") {
                for (var i = 0; i < page.userList.length; i++) {
                    if (page.userList[i].id == this) {
                        html.push(page.userList[i].name);
                        break;
                    }
                }
            }
        });
        return html.join("<br />");
    },

    setWhiteList: function (id) {
        var auction = null;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == id) {
                auction = page.list[i];
                break;
            }
        }

        if (auction == null) {
            alert("数据异常，请刷新页面后再试");
            return false;
        }
        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "设置抽中池",
            contents: '<span id="usercontrol"></span><p><input type="button" onclick="page.setLuckyOrForbidden(' + id + ',1)" class="btn" value="确定" /></p>',
            w: 500,
            y: $(window).scrollTop() + 60,
            drag: true,
            h: 500
        });
        page.popU.showPop();

        var userList = [];
        for (var i = 0; i < page.userList.length; i++) {
            if (auction.Participants.indexOf("," + page.userList[i].id + ",") > -1)
                userList.push(page.userList[i]);
        }

        page.userControl = new NDUAP.Selector({ divID: "usercontrol", typeID: 1, totalSelectNum: 10, width: 450, searchData: userList, showMoreBtn: true });

        if (auction.Lucky && auction.Lucky != "") {
            var lucky = [];
            for (var i = 0; i < userList.length; i++) {
                if (auction.Lucky.indexOf("," + userList[i].id + ",") > -1)
                    lucky.push(userList[i]);
            }
            page.userControl.setValue(lucky);
        }
    },

    setLuckyOrForbidden: function (id, type) {
        var array = [];
        $.each(page.userControl.getValue(), function (i, n) {
            array.push(n.id);
        });
        $.post(page.url + "setLuckyOrForbidden", { id: id, type: type, list: array.join(',') }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.popU.close();
            page.getList();
        }, "json");
    },

    cancel: function (id) {
        if (confirm("确定删除该抽奖么？")) {
            $.post(page.url + "cancel", { id: id }, function (data) {
                if (data.error) {
                    alert(data.error);
                    return false;
                }
                page.getList();
            }, "json");
        }
    },

    setBlackList: function (id) {
        var auction = null;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == id) {
                auction = page.list[i];
                break;
            }
        }

        if (auction == null) {
            alert("数据异常，请刷新页面后再试");
            return false;
        }

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "设置抽不中池",
            contents: '<span id="usercontrol"></span><p><input type="button" onclick="page.setLuckyOrForbidden(' + id + ',2)" class="btn" value="确定" /></p>',
            w: 500,
            y: $(window).scrollTop() + 60,
            drag: true,
            h: 500
        });
        page.popU.showPop();

        var userList = [];
        for (var i = 0; i < page.userList.length; i++) {
            if (auction.Participants.indexOf("," + page.userList[i].id + ",") > -1)
                userList.push(page.userList[i]);
        }

        page.userControl = new NDUAP.Selector({ divID: "usercontrol", typeID: 1, totalSelectNum: 10, width: 450, searchData: userList, showMoreBtn: true });
        if (auction.Forbidden && auction.Forbidden != "") {
            var lucky = [];
            for (var i = 0; i < userList.length; i++) {
                if (auction.Forbidden.indexOf("," + userList[i].id + ",") > -1)
                    lucky.push(userList[i]);
            }
            page.userControl.setValue(lucky);
        }
    }
};

